package com.briup.cms.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.briup.cms.bean.CmsUser;
import com.briup.cms.bean.extend.CmsUserExtend;
import com.briup.cms.service.ICmsUserService;
import com.briup.cms.util.JwtUtil;
import com.briup.cms.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "用户模块")
@RestController
@Slf4j
@RequestMapping("/auth/user")
public class UserController {
    @Autowired
    private ICmsUserService userService;

    //省略...
    @ApiOperation(value = "新增用户", notes = "username、password 必须存在不为空，且username唯一")
    @PostMapping("/save")
    public Result save(@RequestBody CmsUser user) {
        userService.save(user);
        return Result.success("新增成功");
    }

    @ApiOperation("获取用户个人信息")
    @GetMapping("/info")
    public Result getInfo(HttpServletRequest request) {
        String jwt = request.getHeader("Authorization");
        log.info("jwt :  {} ", jwt);
        Long id = JwtUtil.getUserId(jwt);
        log.info("id :  {} ", id);
        return Result.success(userService.queryById(id));
    }

    @ApiOperation(value = "根据id查找用户", notes = "id必须存在且有效")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required
                    = true, paramType = "path", dataTypeClass = String.class)
    })
    @GetMapping("/queryById/{id}")
    public Result queryById(@PathVariable Long id) {
        CmsUser user = userService.queryById(id);
        return Result.success(user);
    }

    @ApiOperation(value = "设置用户为Vip", notes = "id存在且有效")
    @PutMapping("/setVip/{id}")
    public Result setVip(@PathVariable Long id) {
        userService.setVip(id);
        return Result.success("更新成功");
    }

    @ApiOperation(value = "更新用户信息", notes = "id必须存在且有效，如果 username存在则必须唯一")
    @PutMapping("/update")
    public Result update(@RequestBody CmsUser user) {
        System.out.println("UserController.update");
        userService.update(user);
        return Result.success("更新成功");
    }

    @ApiOperation(value = "根据id删除用户", notes = "id必须存在且有效")
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result deleteByBatch(@PathVariable("ids") List<Long> ids) {
        userService.deleteByBatch(ids);
        return Result.success("删除成功");
    }

    @ApiOperation(value = "分页+条件查询用户 ", notes = "用户中要含角色信息 ")

    @GetMapping("/query")
    public Result query(Integer pageNum, Integer pageSize, String username, String status, Integer roleId, Integer isVip) {
        System.out.println("pageNum = " + pageNum + ", pageSize = " + pageSize + ", username = " + username + ", status = " + status + ", roleId = " + roleId + ", isVip = " + isVip);
        IPage<CmsUserExtend> p = userService.query(pageNum, pageSize, username, status, roleId, isVip);

        return Result.success(p);
    }
}